package com.company;
import java.io.*;
import java.util.Arrays;
public class Main {
    //十六进制byte数据转化成10进制数
    public static int TwoByteToDecimal(byte a, byte b){
        return a * 16 + b;
    }
    public static void main(String[] args) throws IOException, InterruptedException {
	// write your code here
        byte[] buffer = {0x6B,0x03,0x00,0x30,0x00,0x08,0x4D,0x09};
        String ip = "192.168.0.80";
        int port = 1234;
        TCPClient tcpClient = new TCPClient(ip,port){

            @Override
            protected void onDataReceive(byte[] bytes, int size) {
                //将设备的数据转化成温湿度信息
                String content = "TCPServer say : " +Arrays.toString(bytes);
                System.out.println(content);
                System.out.println(
                        "Temprature : " + TwoByteToDecimal(bytes[3],bytes[4]) + "\n" +
                                "Humidity : " + TwoByteToDecimal(bytes[5],bytes[6])
                );
            }
        };
        tcpClient.connect();//连接TCPClient
        if (tcpClient.isConnected()){
            //轮询发送数据,buffer是要发送的指令，time单位为毫秒，轮询间隔的时间
            tcpClient.sendEveryTime(buffer,1000);
        }
        tcpClient.close();
    }
}
